import javax.print.attribute.standard.Finishings;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-09
 * Time: 18:26
 */
public class Main {

    //自己写的运行不了
    public int minSubArrayLen1(int target, int[] nums) {
        int n = nums.length;
        int sum = 0;
        int min = 100;
        for (int left = 0; left < n; left++) {
            int right = left + 1;
            int count = 0;
            while (right < n){
                if (nums[left] + nums[right] + sum< target){
                    sum += nums[right];
                    right++;
                    count++;
                }else if (nums[left] + nums[right] + sum > target){
                    break;
                }else {
                    if (count < min){
                        min = count;
                    }
                    count = 0;
                }
            }
        }
        return min;
    }

    //老师写的
    public int minSubArrayLen(int target, int[] nums){
        int n = nums.length,sum = 0,len = Integer.MAX_VALUE;
        for (int left = 0,right = 0; right < n; right++) {
            sum += nums[right];//进窗口
            while (sum >= target){ //判断
                len = Math.min(len,right - left + 1);//更新结果
                sum -= nums[left];
                left++;//出窗口
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }
}
